package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.PcTeamConfig;
import com.ruoyi.db.domain.system.dto.MpClientFriendCount;
import com.ruoyi.db.domain.web.dto.MpRegistForm;
import com.ruoyi.db.mapper.system.PcTeamConfigMapper;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.system.service.IMpClientService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 【商城用户】Controller
 *
 * @author ruoyi
 * @date 2022-07-22
 */
@Api(value = "【商城用户】",tags ="【商城用户】")
@RestController
@RequestMapping("/client")
public class MpClientController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpClientController.class);
    @Autowired
    private IMpClientService mpClientService;

    /**
     * 查询【商城用户】列表
     */
    @ApiOperation(value = "查询【商城用户】列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="【商城用户】",paramType="form")
    })
    @RequiresPermissions("system:client:list")
    @GetMapping("/list")
    public TableDataInfo list(MpClient mpClient)
    {
        try {
            startPage();
            List<MpClient> list = mpClientService.selectMpClientList(mpClient);
            return getDataTable(list);
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return null;
    }
    @GetMapping("/list11")
    public Integer list1(MpClient mpClient)
    {
        try {
            Integer a = mpClientService.test1();
            return a;
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return null;
    }
    /**
     * 查询【团队用户】列表
     */
    @ApiOperation(value = "查询【商城用户团队】列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="【商城用户】",paramType="form"),
            @ApiImplicitParam(name="type",value="123各级用户列表",paramType="query")
    })
    @RequiresPermissions("system:client:teamList")
    @GetMapping("/list/team")
    public TableDataInfo teamList(Long clientId,Integer type,Integer pageNum,Integer pageSize)
    {
        try {
            MpClient mpClient = new MpClient();
            mpClient.setClientId(clientId);
            List<MpClient> list = mpClientService.selectMpClientTeamList(mpClient,type,pageNum,pageSize);
            return getDataTable(list);
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return null;
    }
    /**
     * 查询【团队用户各级人数】
     */
    @ApiOperation(value = "查询【团队用户各级人数】")
    @ApiImplicitParams({
            @ApiImplicitParam(name="clientId",value="【商城用户ID】",paramType="query")
    })
    @RequiresPermissions("system:client:teamCount")
    @GetMapping("/team/count")
    public AjaxResult teamCount(Long clientId)
    {
        try {
            MpClientFriendCount count = mpClientService.selectMpClientTeamCount(clientId);
            return AjaxResult.success(count);
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 导出【商城用户】列表
     */
    @ApiOperation(value = "导出【商城用户】列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="【商城用户】",dataType="MpClient")
    })
    @RequiresPermissions("system:client:export")
    @Log(title = "【商城用户】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody MpClient mpClient)
    {
        try {
            List<MpClient> list = mpClientService.selectMpClientList(mpClient);
            ExcelUtil<MpClient> util = new ExcelUtil<MpClient>(MpClient.class);
            util.exportExcel(response, list, "【商城用户】数据");
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
    }

    /**
     * 获取【商城用户】详细信息
     */
    @ApiOperation(value = "获取【商城用户】详细信息")
    @ApiImplicitParam(name="clientId",value="【商城用户】")
    @RequiresPermissions("system:client:query")
    @GetMapping(value = "/{clientId}")
    public AjaxResult getInfo(@PathVariable("clientId") Long clientId)
    {
        try {
            return AjaxResult.success(mpClientService.selectMpClientByClientId(clientId));
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return AjaxResult.error("【商城用户】异常");
    }

    /**
     * 新增【商城用户】
     */
    @ApiOperation(value = "生成虚拟号")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="生成虚拟号",dataType="MpClient")
    })
    @RequiresPermissions("system:client:add:virtual")
    @Log(title = "生成虚拟号", businessType = BusinessType.INSERT)
    @PostMapping("/add/virtual")
    public AjaxResult addVirtual(@RequestBody MpRegistForm form)
    {
        try {
            mpClientService.insertMpClient(form);
            return AjaxResult.success("添加成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改【商城用户】
     */
    @ApiOperation(value = "修改【商城用户】")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="【商城用户】",dataType="MpClient"),
            @ApiImplicitParam(name="type",value="【修改类型按123456顺序】",dataType="query")
    })
    @RequiresPermissions("system:client:edit")
    @Log(title = "【商城用户】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpClient mpClient)
    {
        try {
            int i=mpClientService.updateMpClient(mpClient);
            return toAjax(i);
        }catch (Exception e){
            log.error("修改【商城用户】发生异常",e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除【商城用户】
     */
    @ApiOperation(value = "删除【商城用户】")
    @ApiImplicitParam(name="clientIds",value="【商城用户】")
    @RequiresPermissions("system:client:remove")
    @Log(title = "【商城用户】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clientIds}")
    public AjaxResult remove(@PathVariable Long[] clientIds)
    {
        try {
            int i=mpClientService.deleteMpClientByClientIds(clientIds);
            return toAjax(i);
        }catch (Exception e){
            log.error("删除【商城用户】发生异常",e);
        }
        return AjaxResult.error("【商城用户】异常");
    }
    /**
     * 查询【商城用户】列表
     */
    @ApiOperation(value = "查询v3【商城用户】列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="【商城用户】",paramType="form")
    })
    @GetMapping("/list/three")
    public TableDataInfo listThree(MpClient mpClient,Integer pageNum,Integer pageSize)
    {
        try {
            List<MpClient> list = mpClientService.selectMpClientListThree(mpClient,pageNum,pageSize);
            return getDataTable(list);
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return null;
    }
    /**
     * 新增运营中心,分公司,事业部
     */
    @GetMapping("/get/bindCenter")
    public TableDataInfo getBindCenter(MpClient mpClient,Integer pageNum,Integer pageSize)
    {
        List<MpClient> list = mpClientService.getCenterUsers(mpClient,pageNum,pageSize);
        return getDataTable(list);
    }
    /**
     * 新增【商城用户】
     */
    @ApiOperation(value = "添加用户号")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClient",value="生成虚拟号",dataType="MpClient")
    })
    @RequiresPermissions("system:client:add")
    @Log(title = "添加账号", businessType = BusinessType.INSERT)
    @PostMapping("/add/client")
    public AjaxResult addClient(@RequestBody MpRegistForm form)
    {
        try {
            mpClientService.insertClient(form);
            return AjaxResult.success("添加成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 获取首页统计数据
     */
    @GetMapping("/get/report")
    public AjaxResult getReport(Boolean isThisWeek,Boolean isThisMonth,Boolean isToday,String startTime,String endTime)
    {
        if(isThisWeek==null){
            isThisWeek=false;
        }
        if(isThisMonth==null){
            isThisMonth=false;
        }
        if(isToday==null){
            isToday=false;
        }
        Map<String,Object> map = mpClientService.getReport(isThisWeek,isThisMonth,isToday,startTime,endTime);
        return AjaxResult.success(map);
    }

}
